/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stargate;

import java.util.Enumeration;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;

/**
 * @author jildefonso
 */
public class Contactos extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private List PIMLists;
    private List FieldsList;
    private List FieldValue;
    private Command exitCommand;
    private Command okCommand;
    private Command backCommand;
    private Command exitCommand1;
    private Command okCommand1;
    private Command exitCommand2;
    private Command backCommand1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Contactos constructor.
     */
    public Contactos() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        ContactList contactList = null;
        try {
            // write pre-action user code here
            contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
            if(contactList != null) {
                Enumeration contacts = contactList.items();
                while(contacts.hasMoreElements()) {
                    Contact c = (Contact) contacts.nextElement();
                    getPIMLists().append(c.getString(Contact.TEL, 0), null);
                }
            }
        } catch (PIMException ex) {
            ex.printStackTrace();
            contactList = null;
        }
        switchDisplayable(null, getPIMLists());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == FieldValue) {//GEN-BEGIN:|7-commandAction|1|35-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|35-preAction
                // write pre-action user code here
                FieldValueAction();//GEN-LINE:|7-commandAction|2|35-postAction
                // write post-action user code here
            } else if (command == backCommand1) {//GEN-LINE:|7-commandAction|3|43-preAction
                // write pre-action user code here
                FieldValue = null;
                switchDisplayable(null, getFieldsList());//GEN-LINE:|7-commandAction|4|43-postAction
                // write post-action user code here
            } else if (command == exitCommand2) {//GEN-LINE:|7-commandAction|5|41-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|41-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|23-preAction
        } else if (displayable == FieldsList) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|7|23-preAction
                // write pre-action user code here
                FieldsListAction();//GEN-LINE:|7-commandAction|8|23-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|9|29-preAction
                // write pre-action user code here
                FieldsList = null;
                switchDisplayable(null, getPIMLists());//GEN-LINE:|7-commandAction|10|29-postAction
                // write post-action user code here
            } else if (command == exitCommand1) {//GEN-LINE:|7-commandAction|11|31-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|12|31-postAction
                // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|13|38-preAction
                // write pre-action user code here
                switchDisplayable(null, getFieldValue());//GEN-LINE:|7-commandAction|14|38-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|16-preAction
        } else if (displayable == PIMLists) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|15|16-preAction
                // write pre-action user code here
                PIMListsAction();//GEN-LINE:|7-commandAction|16|16-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|17|20-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|18|20-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|19|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getFieldsList());//GEN-LINE:|7-commandAction|20|26-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|7-postCommandAction
        }//GEN-END:|7-commandAction|21|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|22|
    //</editor-fold>//GEN-END:|7-commandAction|22|
    //</editor-fold>
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: PIMLists ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of PIMLists component.
     * @return the initialized component instance
     */
    public List getPIMLists() {
        if (PIMLists == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here

            PIMLists = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|14-getter|1|14-postInit
            PIMLists.addCommand(getExitCommand());
            PIMLists.addCommand(getOkCommand());
            PIMLists.setCommandListener(this);
            PIMLists.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return PIMLists;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: PIMListsAction ">//GEN-BEGIN:|14-action|0|14-preAction
    /**
     * Performs an action assigned to the selected list element in the PIMLists component.
     */
    public void PIMListsAction() {//GEN-END:|14-action|0|14-preAction
        // enter pre-action user code here
        String __selectedString = getPIMLists().getString(getPIMLists().getSelectedIndex());//GEN-LINE:|14-action|1|14-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|14-action|2|
    //</editor-fold>//GEN-END:|14-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|19-getter|0|19-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|19-getter|1|19-postInit
            // write post-init user code here
        }//GEN-BEGIN:|19-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|19-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: FieldsList ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of FieldsList component.
     * @return the initialized component instance
     */
    public List getFieldsList() {
        if (FieldsList == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            FieldsList = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|22-getter|1|22-postInit
            FieldsList.addCommand(getBackCommand());
            FieldsList.addCommand(getExitCommand1());
            FieldsList.addCommand(getOkCommand1());
            FieldsList.setCommandListener(this);//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
            try {
                int index = PIMLists.getSelectedIndex();
                ContactList contacts = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
                Enumeration items = contacts.items();
                int i = 0 ;
                Contact contact = null;
                while(items.hasMoreElements() && i <= index) {
                    i++;
                    contact = (Contact) items.nextElement();
                }
                if(contact != null) {
                    int[] fields = contact.getFields();
                    for(int j = 0; j < fields.length; j++)
                        FieldsList.append("" + fields[j], null);
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }//GEN-BEGIN:|22-getter|2|
        return FieldsList;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: FieldsListAction ">//GEN-BEGIN:|22-action|0|22-preAction
    /**
     * Performs an action assigned to the selected list element in the FieldsList component.
     */
    public void FieldsListAction() {//GEN-END:|22-action|0|22-preAction
        // enter pre-action user code here
        String __selectedString = getFieldsList().getString(getFieldsList().getSelectedIndex());//GEN-LINE:|22-action|1|22-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|22-action|2|
    //</editor-fold>//GEN-END:|22-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: FieldValue ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of FieldValue component.
     * @return the initialized component instance
     */
    public List getFieldValue() {
        if (FieldValue == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            FieldValue = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|34-getter|1|34-postInit
            FieldValue.addCommand(getExitCommand2());
            FieldValue.addCommand(getBackCommand1());
            FieldValue.setCommandListener(this);//GEN-END:|34-getter|1|34-postInit
            // write post-init user code here
            try {
                ContactList contacts = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
                int index = PIMLists.getSelectedIndex();
                Enumeration enumeration = contacts.items();
                int i = 0;
                Contact contact = null;
                while(enumeration.hasMoreElements() && i <= index) {
                    i++;
                    contact = (Contact) enumeration.nextElement();
                }
                if(contact != null) {
                    index = FieldsList.getSelectedIndex();
                    int[] fields = contact.getFields();
                    int field = fields[index];
                    try {
                        FieldValue.append("String: " + contact.getString(field, 0), null);
                    } catch(Exception ex2) {}
                    try {
                        FieldValue.append("Binary: " + new String(contact.getBinary(field, 0)), null);
                    } catch(Exception ex2) {}
                    try {
                        FieldValue.append("Boolean: " + contact.getBoolean(field, 0), null);
                    } catch(Exception ex2) {}
                    try {
                        FieldValue.append("Date: " + contact.getDate(field, 0), null);
                    } catch(Exception ex2) {}
                    try {
                        FieldValue.append("Integer: " + contact.getInt(field, 0), null);
                    } catch(Exception ex2) {}
                    try {
                        String[] data = contact.getStringArray(field, 0);
                        String cadena = "";
                        for(int j = 0; j < data.length; j++)
                            cadena += data[j] + ",";
                        FieldValue.append("String[]: " + cadena, null);
                    } catch(Exception ex2) {}
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }//GEN-BEGIN:|34-getter|2|
        return FieldValue;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: FieldValueAction ">//GEN-BEGIN:|34-action|0|34-preAction
    /**
     * Performs an action assigned to the selected list element in the FieldValue component.
     */
    public void FieldValueAction() {//GEN-END:|34-action|0|34-preAction
        // enter pre-action user code here
        String __selectedString = getFieldValue().getString(getFieldValue().getSelectedIndex());//GEN-LINE:|34-action|1|34-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|34-action|2|
    //</editor-fold>//GEN-END:|34-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of exitCommand2 component.
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            exitCommand2 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return exitCommand2;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            backCommand1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
